﻿
@{
    ViewData["Title"] = "MoveShow";
    Layout = "~/Views/Shared/Template.cshtml";
}

<div style="padding: 16px;margin-bottom:10px">
    <h3><span style="color:gray">移库管理</span></h3>
</div>
<hr>
<br />

<h4>数据筛选</h4>
<br />
<hr>

<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">移库单号:</label>
            <div class="layui-input-block">
                <input type="text" id="moveTheLibraryNos" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">移库类型:</label>
            <div class="layui-input-block">
                <select id="moveTheLibraryTypes" lay-filter="aihao">
                    <option value="">请选择</option>
                    <option value="移库上架">移库上架</option>
                    <option value="仓库移库">仓库移库</option>
                    <option value="报损移库">报损移库</option> 
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">单据状态:</label>
            <div class="layui-input-block">
                <select id="auditStatuss" lay-filter="aihao">
                    <option value="">请选择</option>
                    <option value="全部">全部</option>
                    <option value="等待审核">等待审核</option>
                    <option value="审核成功">审核成功</option>
                    <option value="审核失败">审核失败</option>
                </select>
            </div>
        </div>

        <div class="layui-inline">
            <div class="layui-btn-container">
                <button type="button" class="layui-btn layui-bg-blue" onclick="Show()">
                    <i class="layui-icon layui-icon-search"></i>查询
                </button>
                <button type="reset" class="layui-btn layui-btn-primary">
                    <i class="layui-icon layui-icon-refresh" nclick="location.href='/Warehouse/MoveShow'"></i>重置
                </button>
            </div>
        </div>

        <hr />
        <hr />
        <div style="float:left">
            <h4>数据列表</h4>
        </div>
        <div style="float:right">
            <div class="layui-btn-container">
                <button type="button" class="layui-btn layui-bg-blue" lay-on="test-page-custom" onclick="location.href='/Warehouse/MoveAdd'">
                    <i class="layui-icon layui-icon-add-1"></i>新增
                </button>
                <button type="button" class="layui-btn layui-bg-blue" id="batchDeleteButton">
                    <i class="layui-icon layui-icon-delete"></i>删除
                </button>
                <button type="button" class="layui-btn layui-bg-blue" onclick="location.href='/Warehouse/MoveShow'">
                    <i class="layui-icon layui-icon-refresh"></i>刷新
                </button>
            </div>
        </div>
    </div>
</form>
<div>
    <table id="MoveTable"></table>
    <script type="text/html" id="toolbarDemo"></script>
</div>
<hr>
<br />

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-bg-blue layui-btn-sm"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-bg-red layui-btn-sm" ><i class="layui-icon layui-icon-delete" ></i>删除</a>
</script>
<div id="demo-laypage-all"></div>

<script>
    var obj = {

        moveTheLibraryNo: '',
        productName: '',
        productNo: '',
        batch: '',
        productSpec: '',
        outStorageNum: '',
        warehouseName: '',
        warehouseLocationName:'',
        binLocationName: '',
        binLocationId: '',
        moveTheLibraryType: '',
        auditStatus: '',
        examinePeople: '',
        examineTime: '',
        office_id: '',
        user_id: ''
    }
    $(function () {

        Show()
    })
    var pageIndex = 1;
    var pageSize = 100;
    var totalCount = 0;

    //单据状态下拉框
    layui.use('form', function () {
        var form = layui.form;
        $.ajax({
            url: "/Warehouse/GetAduiStatusShow",
            type: "get",
            data: { token: localStorage.getItem("token") },
            success: function (data) {
                var data = JSON.parse(data);
                //console.log(data)
                var options = '<option value="">请选择</option>'; // 添加默认选项
                $.each(data, function (index, item) {
                    options += '<option value="' + item.value + '">' + item.text + '</option>';
                });
                $("#auditStatus").html(options);
                form.render('select'); // 重新渲染下拉框
            },
            error: function () {
                console.log("获取数据失败");
                // 可以考虑显示一个错误消息给用户
            }
        });
    });

    //显示
    function Show() {
        layui.use('table', function () {
            var table = layui.table;
            var laypage = layui.laypage;

            //查询
            var moveTheLibraryNo = $("#moveTheLibraryNos").val();
            var moveTheLibraryType = $("#moveTheLibraryTypes").val();
            var auditStatus = $("#auditStatuss").val();
            $.ajax({
                url: '/Warehouse/GetMoveShow',
                type: "get",
                data: {
                    pageIndex: 1,
                    pageSize: 100,
                    moveTheLibraryNo: moveTheLibraryNo,
                    moveTheLibraryType: moveTheLibraryType,
                    auditStatus: auditStatus||0,
                    token: localStorage.getItem("token"),
                },
                success: function (res) {
                    var res = JSON.parse(res);
                    //console.log(res);
                    if (res.code == 0) {
                        //console.log(res.data.list)
                        //alert(res.data.list.outStorageNum)
                        table.render({
                            elem: MoveTable,//对应HTML中表格的id
                            data: res.data.list,//绑定获取的数据
                            toolbar: '#toolbarDemo',
                            defaultToolbar: ['exports'],
                            cols: [[
                                { type: 'checkbox', fixed: 'left' },
                                { field: 'moveTheLibraryNo', title: '移库单号', width: 100 },
                                { field: 'productName', title: '产品名称', width: 130 },
                                { field: 'productNo', title: '产品编号', width: 110 },
                                { field: 'batch', title: '批次', width: 110 },
                                { field: 'productSpec', title: '规格', width: 110 },
                                { field: 'outStorageNum', title: '数量', width: 110 },
                                { field: 'warehouseName', title: '仓库', width: 110 },
                                { field: 'binLocationName', title: '原库位', width: 110 },
                                { field: 'newBinLocationName', title: '新库位', width: 110 },
                                { field: 'moveTheLibraryType', title: '移库类型', width: 110 },
                                { field: 'auditStatuss', title: '状态', 
                                templet:function(d){
                                    if(d.auditStatus==1)
                                    {
                                        return '等待审核';
                                    }else if(d.auditStatus==2)
                                    {
                                            return '审核成功';
                                    }else if(d.auditStatus==3)
                                    {
                                            return '审核失败';
                                    }
                                }
                                 },
                                { field: 'examinePeople', title: '审核人', width: 110 },
                                { field: 'examineTime', title: '审核时间', width: 110 },
                                {
                                    field: '', title: '操作', width: 200, templet: function (d) {
                                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" lay-on="test-page-edit" onclick=" storagemodel(' + d.moveTheLibraryId + ')"><i class="layui-icon-survey"></i>查看</a> ' +

                                            '<a class="layui-btn layui-btn-sm layui-btn-primary" lay-on=s"test-page-edit" onclick="updateData(' + d.moveTheLibraryId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +

                                            '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.moveProductId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>' +

                                            ' <a class="layui-btn layui-btn-sm layui-btn-primary" lay-on="test-page-edit" onclick="storageaudit(' + d.moveTheLibraryId + ')"> <i class="layui-icon layui-icon-left"></i>审核</a> ';
                                    }
                                }
                            ]],
                            page: true, // 是否显示分页
                            count: totalCount,
                            limits: [3, 6, 8],
                            limit: 3,// 每页默认显示的数量
                            curr: pageIndex,
                        });

                    } else {
                        console.log(res.msg);
                    }
                    totalCount = res.data.totalCount
                    console.log(totalCount)

                },
                error: function () {
                    console.log("获取数据失败");
                    // 可以考虑显示一个错误消息给用户
                }
            });

        });
    }
    //查看详情
    window.storagemodel = function (moveTheLibraryId) {
        // 先根据id获取要编辑的数据，发起Ajax请求
        $.ajax({
            type: "get",
            url: '/Warehouse/GetFanMove', // 替换为实际的后台根据id获取数据的接口地址
            data: {
                moveTheLibraryId: moveTheLibraryId,
                token: localStorage.getItem("token")
            },
            success: function (res) {
                try {
                    const { data } = JSON.parse(res);
                    //console.log(data)
                    if (data) {
                        const { moveProductData } = data;
                        var storage = data;
                        console.log(storage);
                        console.log(moveProductData);
                        // 反填表单的函数
                        fillForm(storage);

                        //渲染表格的函数
                        renderTable(moveProductData);
                        resolve(storage, moveProductData);
                    } else {
                        layui.layer.msg(res.message, { icon: 2 });
                        reject(res.message);
                    }
                } catch (e) {
                    //console.error(e);
                    //layui.layer.msg('数据解析失败，请稍后重试', { icon: 2 });
                    //reject('数据解析失败');
                }
            },
        });

        layer.open({
            type: 1,
            title: '移库单详情',
            content: `
                       <form class="layui-form" action="">
                       <div class="layui-form-item" style="margin-left:150px;margin-top:50px">
             <div class="layui-inline">
                 <label class="layui-form-label">移库单号:</label>
                 <div class="layui-input-inline">
                     <input type="text" id="moveTheLibraryNo" name="moveTheLibraryNo" autocomplete="off" class="layui-input">
                 </div>
             </div>
                 <div class="layui-inline">
                <label class="layui-form-label">移库单类型</label>
                <div class="layui-input-inline">
                 <input type="text" id="moveTheLibraryType" name="moveTheLibraryType" autocomplete="off" class="layui-input">
                </div>
            </div>
             <div class="layui-inline">
                 <label class="layui-form-label">关联订单号</label>
                 <div class="layui-input-inline">
                     <input type="text" id="associateNumber" name="associateNumber" autocomplete="off" class="layui-input">
                 </div>
             </div>
         </div>

         <div class="layui-form-item" style="margin-left:140px;margin-top:30px">
             <div class="layui-inline">
                 <label class="layui-form-label">制单人</label>
                 <div class="layui-input-inline">
                     <input type="text" id="singleMaker" name="singleMaker" autocomplete="off" class="layui-input">
                 </div>
             </div>
             <div class="layui-inline">
                 <label class="layui-form-label">制单日期</label>
                 <div class="layui-input-inline">
                             <input type="text" id="makeTime" name="makeTime" autocomplete="off" class="layui-input">
                 </div>
             </div>

         </div>
         <div class="layui-form-item layui-form-text" style="margin-left:130px;margin-top:30px">
             <label class="layui-form-label">备注</label>
             <div class="layui-input-block">
             <textarea placeholder="请输入内容" name="moveRemark"  id="moveRemark" class="layui-textarea"></textarea>
             </div>
         </div>


                <table class="layui-table" id="allocationProducts">

                <tbody id="data"></tbody>
            </table>
                </div>
                <div id="demo-laypage-layout"></div>`,
            area: ['1400px', '800px'],
            success: function () {

                // 对弹层中的表单进行初始化渲染
                //form.render();
            }
        });
    };

    var obj1 = {
        moveTheLibraryId: 0,
        auditStatus: '',
        examineReason: '',
        examinePeople: '',
        examineTime: '',
    };
    // 反填表单的函数
    function fillForm(storage) {
        // $(parsedData.data).each(function () {
        // console.log(parsedData.data)
        // console.log(this.reportlossesNo)
            $("#moveTheLibraryNo").val(storage.moveTheLibraryNo);
            $("#moveTheLibraryType").val(storage.moveTheLibraryType);
            $("#auditStatus").val(storage.auditStatus);
            $("#associateNumber").val(storage.associateNumber);
            $("#singleMaker").val(storage.singleMaker);
            $("#moveRemark").val(storage.moveRemark);
            $("#examineReason").val(storage.examineReason);
            $("#warehouseName").val(storage.warehouseName);
            const date = new Date(storage.makeTime);
            const year = date.getFullYear();
            const month = ("0" + (date.getMonth() + 1)).slice(-2);
            const day = ("0" + date.getDate()).slice(-2);
            const hours = ("0" + date.getHours()).slice(-2);
            const minutes = ("0" + date.getMinutes()).slice(-2);
            $('#makeTime').val(year + "-" + month + "-" + day);

            obj1.reportlossesId = storage.reportlossesId
            console.log(obj1.productId)
            obj1.moveTheLibraryNo = storage.moveTheLibraryNo
            obj1.moveTheLibraryType = storage.moveTheLibraryType
            obj1.auditStatus = storage.auditStatus
            obj1.associateNumber = storage.associateNumber
            obj1.singleMaker = storage.singleMaker
            obj1.makeTime = storage.makeTime
            obj1.moveRemark = storage.moveRemark
            obj1.examineReason = storage.examineReason

        }


    // 渲染表格的函数
    function renderTable(moveProductData) {
        console.log(moveProductData);
        //var storageId = storageProductData.storageId

        layui.use('table', function () {
            var table = layui.table;
            if (moveProductData) {
                var inst = table.render({
                    elem: '#allocationProducts', // 对应HTML中表格的id
                    data: moveProductData, // 绑定获取的数据
                    cols: [[
                        { field: 'productName', title: '产品名称' },
                        { field: 'productNo', title: '产品编码' },
                        { field: 'productSpec', title: '规格' },
                        { field: 'batch', title: '批次' },
                        { field: 'outStorageNum', title: '数量' },
                        { field: 'binLocationName', title: '库位' },
                    ]],
                });
            }
        });
    }

    // 审批
    window.storageaudit = function (moveTheLibraryId) {
        // 先根据id获取要编辑的数据，发起Ajax请求
        $.ajax({
            type: "get",
            url: "http://10.223.8.18:8000/warehouse/api/Move/Move",  // 替换为实际的后台根据id获取数据的接口地址
            data: {
                moveTheLibraryId: moveTheLibraryId,
                token: localStorage.getItem("token")
            },
            success: function (res) {
                try {
                    const { data } = JSON.parse(res);
                    //console.log(data)
                    if (data) {
                        const { moveProductData } = data;
                        var storage = data;
                        console.log(storage);
                        console.log(moveProductData);
                        // 反填表单的函数
                        fillForm(storage);

                        //渲染表格的函数
                        renderTable(moveProductData);
                        resolve(storage, moveProductData);
                    } else {
                        layui.layer.msg(res.message, { icon: 2 });
                        reject(res.message);
                    }
                } catch (e) {
                    //console.error(e);
                    //layui.layer.msg('数据解析失败，请稍后重试', { icon: 2 });
                    //reject('数据解析失败');
                }
            },
        });

        layer.open({
            type: 1,
            title: '入库单审核',
            content: `
                           <form class="layui-form" action="">
                           <div class="layui-form-item" style="margin-left:150px;margin-top:50px">
                 <div class="layui-inline">
                     <label class="layui-form-label">移库单号:</label>
                     <div class="layui-input-inline">
                         <input type="text" id="moveTheLibraryNo" name="moveTheLibraryNo" autocomplete="off" class="layui-input">
                     </div>
                 </div>
                     <div class="layui-inline">
                    <label class="layui-form-label">移库单类型</label>
                    <div class="layui-input-inline">
                     <input type="text" id="moveTheLibraryType" name="moveTheLibraryType" autocomplete="off" class="layui-input">
                    </div>
                </div>
                 <div class="layui-inline">
                     <label class="layui-form-label">关联订单号</label>
                     <div class="layui-input-inline">
                         <input type="text" id="associateNumber" name="associateNumber" autocomplete="off" class="layui-input">
                     </div>
                 </div>
             </div>

             <div class="layui-form-item" style="margin-left:140px;margin-top:30px">
                 <div class="layui-inline">
                     <label class="layui-form-label">制单人</label>
                     <div class="layui-input-inline">
                         <input type="text" id="singleMaker" name="singleMaker" autocomplete="off" class="layui-input">
                     </div>
                 </div>
                 <div class="layui-inline">
                     <label class="layui-form-label">制单日期</label>
                     <div class="layui-input-inline">
                                 <input type="text" id="makeTime" name="makeTime" autocomplete="off" class="layui-input">
                     </div>
                 </div>

             </div>
             <div class="layui-form-item layui-form-text" style="margin-left:130px;margin-top:30px">
                 <label class="layui-form-label">备注</label>
                 <div class="layui-input-block">
                 <textarea placeholder="请输入内容" name="moveRemark"  id="moveRemark" class="layui-textarea"></textarea>
                 </div>
             </div>


                    <table class="layui-table" id="allocationProducts">

                    <tbody id="data"></tbody>
                </table>
                    </div>
                    <div id="demo-laypage-layout"></div>`,
            area: ['1500px', '900px'],
            success: function () {
                var obj1 = {
                    moveTheLibraryId: 0,
                    auditStatus: '',
                    examineReason: '',
                    examinePeople: '',
                };
                // 对弹层中的表单进行初始化渲染
                //form.render();
                // 审核通过
                form.on('submit(demo-login)', function (data) {
                    var field = data.field;
                    console.log(obj)
                    obj1.moveTheLibraryId = moveTheLibraryId
                    console.log(obj1.storageId)
                    obj1.examineReason = field.examineReason
                    console.log(obj1.examineReason)
                    obj1.auditStatus = 2
                    //$.ajax({
                    //    url: '/Warehouse/GetStorageAudit',
                    //    type: 'post',
                    //    data: { obj1, token: localStorage.getItem("token") },
                    //    success: function (res) {
                    //        var res = JSON.parse(res)
                    //        console.log(field)
                    //        console.log(res)
                    //        //if (res.code == 1) {
                    //        //    layer.alert("审批成功");
                    //        //    setTimeout(function () {
                    //        //        location.reload();
                    //        //    }, 2000);
                    //        //}
                    //        //else {
                    //        //    layer.alert("操作失败");
                    //        //}
                    //    }
                    //})
                    //return false; // 阻止默认 form 跳转
                });
                // 审核不通过
                form.on('submit(demo-false)', function (data) {
                    var field = data.field;
                    obj1.moveTheLibraryId = obj.moveTheLibraryId
                    obj1.examineReason = field.examineReason
                    obj1.auditStatus = 3
                    //$.ajax({
                    //    url: '/Warehouse/GetStorageAudit',
                    //    type: 'post',
                    //    data: { obj1, token: localStorage.getItem("token") },
                    //    success: function (res) {
                    //        var res = JSON.parse(res)
                    //        console.log(field)
                    //        console.log(res)
                    //        //if (res.code == 1) {
                    //        //    layer.alert("审批不通过");
                    //        //    setTimeout(function () {
                    //        //        location.reload();
                    //        //    }, 2000);
                    //        //}
                    //        //else {
                    //        //    layer.alert("操作失败");
                    //        //}
                    //    }
                    //})
                    //return false; // 阻止默认 form 跳转
                });
            }
        });
    };
    //逻辑删除
    window.deleteData = function (moveProductId) {
        console.log(moveProductId)
        layer.confirm('确定要删除该条数据吗？', function (index) {
            $.ajax({
                type: "Post",
                url: "../Warehouse/GetDeleteMove",  // 替换为实际的后台删除数据接口地址
                data: { data: JSON.stringify({ moveProductId: moveProductId }), token: localStorage.getItem("token") },
                success: function (res) {
                    var res = JSON.parse(res)
                    if (res.code === 1) {
                        layer.alert("删除成功");
                        Show();  // 重新加载数据，刷新表格显示
                    } else {
                        layer.alert("删除失败");

                    }
                },
                error: function () {
                    layer.msg('删除失败，请稍后重试', {
                        time: 2000,
                        icon: 2
                    });
                }
            });
            layer.close(index);
        });
    };

    // 点击批量删除按钮的事件处理
    $('#batchDeleteButton').click(function () {
        var table = layui.table;
        // 获取选中行的数据（这里主要是获取warehouseId）
        var checkStatus = table.checkStatus('MoveTable');
        var selectedIds = checkStatus.data.map(function (item) { return item.moveProductId; });
        console.log(selectedIds)
        if (selectedIds.length === 0) {
            layer.msg('请至少选择一条要删除的数据', { icon: 2 });
            return;
        }
        layer.confirm('确定要批量删除选中的 ' + selectedIds.length + ' 条数据吗？', function (index) {
            // 发送Ajax请求到后端，传递选中的id数组进行删除操作
            var moveProductId = {
                moveProductId: selectedIds
            }
            console.log(MoveTable)
            $.ajax({
                url: '/Warehouse/GetPlDelectMove', // 后端批量删除接口地址，根据实际调整
                type: 'post',
                data: {
                    moveProductId: moveProductId,
                    token: localStorage.getItem("token"),
                },
                success: function (res) {
                    var res = JSON.parse(res)
                    console.log(res)
                    if (res.code > 0) {
                        layer.msg('批量删除成功', { icon: 1 });
                        Show()
                    } else {
                        layer.msg('批量删除失败：' + res.message, { icon: 2 });
                    }
                    layer.close(index);
                },
                error: function () {
                    layer.msg('请求出错，请稍后再试', { icon: 2 });
                    layer.close(index);
                }
            });
        });
    });

    // 点击编辑按钮的事件处理
    $('#batchUpdBtn').click(function () {
        var table = layui.table;
        // 获取选中行的数据（这里主要是获取warehouseId）
        var checkStatus = table.checkStatus('MoveTable');
        var selectedData = checkStatus.data.map(function (item) {
            return item.moveTheLibraryId;
        });
        console.log(selectedData);
        if (selectedData.length === 0) {
            layer.msg("请选择一条要编辑的数据", { icon: 2 });
            return;
        }
        localStorage['moveTheLibraryId'] = selectedData;
        location.href = '/Warehouse/MoveUpd';
    });

    //移库编辑
    window.updateData = function (moveTheLibraryId) {
        localStorage['moveTheLibraryId'] = moveTheLibraryId;
        location.href = '/Warehouse/MoveUpd';
    }

</script>